package top.stmo.stmoBlog.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.stmo.stmoBlog.dao.mapper.ArticleMapper;
import top.stmo.stmoBlog.dao.pojo.Article;
import top.stmo.stmoBlog.dao.pojo.ArticleBody;
import top.stmo.stmoBlog.dao.vo.ArtivleVo;
import top.stmo.stmoBlog.dao.vo.params.PageParams;
import top.stmo.stmoBlog.service.ArticleBodyService;
import top.stmo.stmoBlog.service.ArticleService;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleBodyService articleBodyService;


    @Override
    public List<ArtivleVo> findIndexArticle() {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.select(Article::getId,Article::getTitle,Article::getSummary,Article::getArticleImg,Article::getCreateDate);
        queryWrapper.orderByDesc(Article::getViewCounts);
        queryWrapper.last("limit "+3);
        List<Article> articleList = articleMapper.selectList(queryWrapper);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<ArtivleVo> artivleVoList = new ArrayList<>();

        for(Article article : articleList) {
            String str = dateFormat.format(article.getCreateDate());
            ArtivleVo artivleVo = new ArtivleVo();
            artivleVo.setId(article.getId());
            artivleVo.setTitle(article.getTitle());
            artivleVo.setSummary(article.getSummary());
            artivleVo.setArticleImg(article.getArticleImg());
            artivleVo.setCreateDate(str);
            artivleVo.setArticleImg(article.getArticleImg());
            artivleVoList.add(artivleVo);
        }
        return artivleVoList;
    }

    @Override
    public List<ArtivleVo> findAllArticle(PageParams pageParams) {

        Page<Article> page = new Page<>(pageParams.getPage(), pageParams.getPageSize());
        IPage<Article> articleIPage = articleMapper.listArticle(page,pageParams.getCategoryId(),pageParams.getQuery());
        List<Article> records = articleIPage.getRecords();
        pageParams.setTotal(articleIPage.getTotal());
        List<ArtivleVo> artivleVoList = copyList(records);
        return artivleVoList;
    }

    @Override
    public List<ArtivleVo> findHotArticle() {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.select(Article::getId,Article::getTitle);
        queryWrapper.orderByDesc(Article::getViewCounts);
        queryWrapper.last("limit "+8);
        List<Article> articleList = articleMapper.selectList(queryWrapper);
        List<ArtivleVo> artivleVoList = new ArrayList<>();
        for(Article article : articleList) {
          ArtivleVo artivleVo = new ArtivleVo();
          artivleVo.setId(article.getId());
          artivleVo.setTitle(article.getTitle());
          artivleVoList.add(artivleVo);
        }
        return artivleVoList;
    }

    @Override
    public ArtivleVo findArticleById(Long id) {
        Article article = articleMapper.selectById(id);
        Long bodyId = article.getBodyId();
        ArticleBody articleBody = articleBodyService.findArticleBodyById(bodyId);
        ArtivleVo artivleVo = new ArtivleVo();
        BeanUtils.copyProperties(article,artivleVo);
        artivleVo.setArticleBody(articleBody);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String str = dateFormat.format(article.getCreateDate());
        String[] creatArray = str.split("-");
        artivleVo.setYear(creatArray[0]);
        artivleVo.setMonth(creatArray[1]);
        artivleVo.setDay(creatArray[2]);
        return artivleVo;
    }

    private List<ArtivleVo> copyList(List<Article> records) {
        List<ArtivleVo> artivleVoList = new ArrayList<>();
        for(Article record : records) {
            ArtivleVo artivleVo = copy(record);
            artivleVoList.add(artivleVo);
        }
        return artivleVoList;
    }

    private ArtivleVo copy(Article record) {
        ArtivleVo artivleVo = new ArtivleVo();
        artivleVo.setId(record.getId());
        artivleVo.setTitle(record.getTitle());
        artivleVo.setSummary(record.getSummary());
        artivleVo.setCommentCounts(record.getCommentCounts());
        artivleVo.setViewCounts(record.getViewCounts());
        artivleVo.setArticleImg(record.getArticleImg());
        artivleVo.setCategoryId(record.getCategoryId());
        artivleVo.setOriginal(record.getOriginal());
        artivleVo.setWeight(record.getWeight());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String str = dateFormat.format(record.getCreateDate());
        String[] diaryArray = str.split("-");
        artivleVo.setYear(diaryArray[0]);
        artivleVo.setMonth(diaryArray[1]);
        artivleVo.setDay(diaryArray[2]);
        return artivleVo;
    }
}
